During this class so far, we have been using the iostream standard library. This library provided us with methods like, cout and​ cin. ​cin method is for reading from standard input​ and ​cout is for writing to standard output.

In this recitation we will discuss ​file Input/Output​, which will allow you to read and write from a file. In order to use these methods, we will need to use another c++ standard library, fstream.

Headers to include:

These headers must be included in your C++ file before you are able to process files. #include <iostream>

#include <fstream>

Opening a file:

The first step to processing files is to open the file, with ​open(​ filename, mode). It must be opened before you are allowed to read from it or write to it. In order to open a file, the

​ 	ofstream​, ​fstream, or ​ifstream objects should be used. If you want to open the file for writing, either the ofstream or fstream object may be used. If you want to open a file for reading only, then the ifstream object should be used. 

	For example: 

ofstream myfile; myfile.open(“filename”);

Checking for open file:

It is always good practice to check if the file has been opened properly or send a message that it did not open properly. To check if a file stream successfully opened the file, you can use​is_open().​This method will return a boolean value true or false.
Reading from a file:

When you read from a file into your C++ program, you will use the stream insertion syntax you have seen with cin, which inputs information from the keyboard or user, ​(>>)​. The difference is that you will be using the ifstream object instead of the cin object, allowing for the program to input or read from the file. Here you would be given a file with information to read. Then once the print statement is called, you will see the files of the lines printed to the terminal.

For example:

getline(myfile, line) cout << line << endl;

Once the file is open and checked you need to get each line of the file. What will you need to be able to get each line of the file?

Writing to a file:

When you write to a file from your C++ program, you will use the stream insertion syntax you have seen with cout, which outputs information to the terminal screen, ​(<<)​. The difference is that you will be using the ofstream object instead of the cout object, allowing for the program to direct the output correctly.

For example:

myfile << “Writing this line to a file. \n”;

myfile << “Writing this second line to a file. \n”;

If you opened the text file, then you will see the string statements within the new output text file, such as:

Writing this line to a file.

Writing this second line to a file.
Closing a file:

When you are finished processing your files, it is always better to close all the opened files before the program is terminated. The standard syntax for closing your file is close();​See the above example used in opening a file.

Recitation 6 exercise:

Write a program that reads the values from a file and writes the sum of all multiples of 2 or 5 into another file. You can use the similar logic from the last recitation.  Your program must contain separate functions for reading and writing a file.